package com.kaka.bus.engine.home.info.borrowlist;

import android.content.Intent;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.kaka.bus.R;
import com.kaka.bus.adapter.BorrowListAdapter;
import com.kaka.bus.base.BaseActivity;
import com.kaka.bus.bean.BorrowList;
import com.kaka.bus.bean.HttpWrapper;
import com.kaka.bus.constant.Constant;
import com.kaka.bus.engine.home.info.borrowlist.deteil.BorrowDeteilActivity;
import com.kaka.bus.listener.Listener;
import com.kaka.bus.util.ToastUtil;
import com.kaka.bus.util.U;
import com.kaka.bus.view.MutipleLayout;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

/**
 * Created by zhangqi on 2017/12/25.
 * e-mail : ${email}
 * desc :
 */

public class BorrowListActivity extends BaseActivity<BorrowListPresenter> implements BorrowListContract.View {

    @BindView(R.id.toolbar_back)
    ImageView mToolbarBack;
    @BindView(R.id.toolbar_name)
    TextView mToolbarName;
    @BindView(R.id.toolbar)
    Toolbar mToolbar;
    @BindView(R.id.borrowlist_recy)
    RecyclerView mBorrowlistRecy;
    @BindView(R.id.borrowlist_smart)
    SmartRefreshLayout mBorrowlistSmart;
    @BindView(R.id.borrowlist_muti)
    MutipleLayout mBorrowlistMuti;

    private int mPage = 1;
    private int mPageSize = 10;

    private BorrowListAdapter mBorrowListAdapter;

    private List<BorrowList> mDatas = new ArrayList<>();

    @Override
    protected void initView() {
        super.initView();
        mToolbarBack.setOnClickListener(v -> finish());
        mToolbarName.setText("我的回收");
//        BorrowList a=new BorrowList();
//        mDatas.add(a);
//        mDatas.add(a);
//        mDatas.add(a);
//        mDatas.add(a);
        mBorrowlistRecy.setLayoutManager(new LinearLayoutManager(this));
        mBorrowListAdapter = new BorrowListAdapter(R.layout.item_myborrow, mDatas);
        mBorrowlistRecy.setAdapter(mBorrowListAdapter);
        mBorrowListAdapter.bindToRecyclerView(mBorrowlistRecy);
        mBorrowlistMuti.setEmptyText("您当前暂无手机回收记录!");
        mBorrowlistMuti.setEmptyImage(R.mipmap.empty_view);
        mBorrowlistMuti.setStatus(MutipleLayout.Loading);

    }

    @Override
    protected void initData() {

        getData();
    }

    public void getData(){
        mPage = 1;
        mDatas.clear();
        mPresenter.initData(getUserId(),getToken(),1,mPageSize);
    }

    @Override
    public void onError() {
        mPage--;
        mBorrowlistSmart.finishLoadmore(0);
        mBorrowlistMuti.setStatus(MutipleLayout.Error);
    }

    @Override
    protected void onRestart() {
        super.onRestart();
        getData();
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        getData();
    }

    @Override
    public void onResultStart(HttpWrapper<List<BorrowList>> data) {
        mBorrowlistSmart.finishRefresh(0);
        if(Constant.SUCCESS_CODE.equals(data.code)){
            if(data.data != null && data.data.size() > 0){
                mBorrowListAdapter.addData(data.data);
                mBorrowlistMuti.setStatus(MutipleLayout.Success);
            }else{
                mBorrowlistMuti.setStatus(MutipleLayout.Empty);
            }
        }else if(Constant.ERROR_CODE.equals(data.getCode())){
            showMessage(data.getInfo());
            onError();
        }else if(Constant.TOKEN_TIMEOUT.equals(data.code)){
            U.tokenOut(this);
        }
    }

    @Override
    public void onResultLoadMore(HttpWrapper<List<BorrowList>> o) {
        mBorrowlistSmart.finishLoadmore(0);
        if(Constant.SUCCESS_CODE.equals(o.getCode())){
                if(o.data != null && o.data.size() > 0){
                    mBorrowListAdapter.addData(o.data);
                }else{
                    showMessage("没有更多数据了");
                    mPage--;
                }
        }else if(Constant.ERROR_CODE.equals(o.getCode())){
            showMessage(o.getInfo());
            onError();
        }else if(Constant.TOKEN_TIMEOUT.equals(o.code)){
            U.tokenOut(this);
        }
    }

    @Override
    public void refresh(HttpWrapper<List<BorrowList>> data) {
        if (Constant.SUCCESS_CODE.equals(data.code)){
            getData();
        }else {
            ToastUtil.showShort(this,"取消失败！");
        }
    }

    @Override
    protected void initListener() {
        mBorrowlistSmart.setOnRefreshListener(refreshlayout -> {
            getData();
        });

        mBorrowlistSmart.setOnLoadmoreListener(refreshlayout -> {
            mPresenter.loadMore(getUserId(),getToken(),++mPage,mPageSize);
        });

        mBorrowListAdapter.setOnItemClickListener((adapter, view, position) -> {
            String number=mDatas.get(position).getBorrStatus().trim();
            if (number.equals("BS004")||number.equals("BS005")||number.equals("BS006")||number.equals("BS014")||number.equals("BS010")) {
                Intent intent = new Intent(this, BorrowDeteilActivity.class);
                intent.putExtra(BorrowDeteilActivity.BORROWID, mDatas.get(position).getId());
                startActivity(intent);
            }
        });
        mBorrowListAdapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
            @Override
            public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
                 if (view.getId()==R.id.cancel_item){
                     U.getSlectDialog(BorrowListActivity.this, "提示", "您确定要取消此回收合同吗？", "确定", "取消", new Listener.OnNormalAlertDialogChooseClickListener() {
                         @Override
                         public void onLeft() {
                             mPresenter.cancelRequest(getUserId(),getToken(),mDatas.get(position).getId()+"");
                         }

                         @Override
                         public void onRight() {

                         }
                     }).show();

                 }
            }
        });
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_borrowlist;
    }

    @Override
    protected void initInject() {
        getActivityComponent().inject(this);
    }

}
